package com.saicmotor.springboot_demo.service.impl;

import com.saicmotor.springboot_demo.mapper.CommonMapper;
import com.saicmotor.springboot_demo.service.CommonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service
public class CommonServiceImpl implements CommonService {
    private static final String TABLE_SCHEMA="springboot_demo";
    @Autowired
    CommonMapper commonMapper;

    @Override
    public boolean checkTable(String tableName) {
        try {
            Integer count = commonMapper.checkTableBySchema(TABLE_SCHEMA,tableName);
            return count==1;
        }catch (Exception e){
            Map<String,String> tableMap=commonMapper.checkTableBySql(tableName);
            return !tableMap.isEmpty();
        }
    }

    @Override
    public boolean checkName(String tableName, String name) {
        if(commonMapper.getIdByName(tableName,name)!=null){
            return true;
        }else {
            return false;
        }
    }

    @Override
    public void cleanTable(String tableName) {
        commonMapper.cleanTable(tableName);
    }

    @Override
    public boolean tableIsEmpty(String tableName) {
        if(commonMapper.tableIsEmpty(tableName)==0){
            return true;
        }else {
            return false;
        }
    }
}
